package com.iskny.framework.admin.controllor;

import com.iskny.framework.admin.interceptor.LoginInterceptor;
import com.iskny.framework.config.Constants;
import com.iskny.framework.model.Menu;
import com.iskny.framework.model.ModelState;
import com.iskny.framework.model.MyPage;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.plugin.ehcache.CacheName;
import com.jfinal.plugin.ehcache.EvictInterceptor;
@Before(LoginInterceptor.class)
public class MenuAction extends Controller {

	private static final Constants constant = Constants.me();
	private Menu menuDao = Menu.dao;
	private int pageSize = constant.getPageSize();
	public void index(){
		MyPage page = menuDao.pageMenu(getParaToInt(0,1),pageSize);
		setAttr("page", page);
		render("menulist.html");
	}
	public void edit(){
		long id = getParaToLong(0,-1L);
		if(id!=-1L){
			Menu menu = menuDao.findById(id);
			setAttr("menu", menu);
		}
		render("menuedit.html");
	}
	@Before({EvictInterceptor.class,MenuValidator.class})
	@CacheName(Menu.menuPageKey)//更新缓存
	public void save(){
		Menu menu = getModel(Menu.class);
		if(menu.get("id")!=null){
			menu.update();
		}else{
			menu.save();
		}
		redirect("/admin/menu");
	}
	@Before(EvictInterceptor.class)
	@CacheName(Menu.menuPageKey)
	public void delete(){
		long id = getParaToLong(0,-1L);
		if(id!=-1L){
			Menu menu = menuDao.findById(id);
			menu.set("state", ModelState.deleted).update();
		}
		redirect("/admin/menu");
	}
}
